/**
 * Created By wifi_uncle on 2019/11/6
 */

import API from '../api'
import Utils from '../utils'
import Config from '../config'
import request from "../request";
export default {
  /**
   * 获取推荐人列表
   * @param premisesId
   * @param _this
   * @returns {PromiseLike<any> | Promise<any> | *}
   */
  getRecommendList (premisesId, _this) {
    let url = Utils.getPrefix(_this) + API.RECOMMEND.RECOMMEND_LIST
    let body = {}
    body.params = {
      // premisesId: premisesId,
      id: premisesId
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        return Promise.resolve(data.resData.list)
      } else {
        Utils.showFailMsg('获取楼盘推荐人失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('获取楼盘推荐人失败')
      return Promise.reject(e)
    })
  },

  /**
   * 保存推荐人
   * (新增、删除同一个接口)
   * @param premisesId
   * @param recommendIdList
   * @param _this
   * @param type    ADD 新增  DELETE 删除
   * @returns {Promise<T>}
   */
  saveRecommend ({premisesId = '', recommendIdList = [], _this, type = 'ADD'}) {
    let url = type === 'ADD' ? API.RECOMMEND.SAVE_RECOMMEND : API.RECOMMEND.DEL_RECOMMEND
    url = Utils.getPrefix(_this) + url
    let body = {}
    body.params = {
      premisesId,
      recommenderId: recommendIdList
    }
    return _this.$http.post(url, body).then(rsp => {
      const data = rsp.data
      if (data && data.result === Config.SUCCESS_CODE) {
        Utils.showSuccessMsg('操作成功')
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('操作失败')
        return Promise.reject(data)
      }
    }).catch(e => {
      Utils.Log(e)
      Utils.Log('操作失败')
      return Promise.reject(e)
    })
  },
  /**
   * 批量同步推荐人
   * @param premisesIdList
   * @param recommendIdList
   * @param _this
   * @returns {Promise<T>}
   */
  batchSyncRecommend ({premisesIdList = [], recommendIdList = [], _this}) {
    let url = Utils.getPrefix(_this) + API.RECOMMEND.BATCH_SYNC_RECOMMEND
    let body = {}
    body.params = {
      premisesIds: premisesIdList,
      recommenderIds: recommendIdList
    }
    return _this.$http.post(url, body).then(rsp => {
      const data = rsp.data
      if (data && data.result === Config.SUCCESS_CODE) {
        Utils.showSuccessMsg('同步成功')
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('同步失败')
        return Promise.reject(data)
      }
    }).catch(e => {
      Utils.Log(e)
      Utils.Log('同步失败')
      return Promise.reject(e)
    })
  },
  getChannelAllRecommendList ({params,_this}) {
    const url = Utils.getPrefix(_this) + API.RECOMMEND.GET_CHANNEL_ALL_RECOMMEND_LIST
    const body = {
      params
    }
    const errorMsg = '获取全部推荐人失败'
    return _this.$http.post(url, body).then(rsp => {
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        return Promise.resolve(data.resData)
      } else {
        Utils.showFailMsg(errorMsg)
        return Promise.reject(data)
      }
    }).catch(e => {
      Utils.Log(e)
      Utils.Log(errorMsg)
      return Promise.reject(e)
    })
  },
  changeCPRecommender ({customerPremisesId, recommenderUuid, _this}) {
    let url = Utils.getPrefix(_this) + API.RECOMMEND.RECOMMENDER_CHANGE
    let body = {}
    body.params = {
      customerPremisesId,
      recommenderUuid
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        Utils.showSuccessMsg('变更推荐人成功')
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('变更推荐人失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('变更推荐人失败')
      return Promise.reject(e)
    })
  },
}
